package com.zone.modular.system.sysuser.service.impl;

import com.zone.modular.system.sysuser.entity.*;
import com.zone.modular.system.sysuser.mapper.ShishigaikuangMapper;
import com.zone.modular.system.sysuser.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZoneWonderful
 * @since 2022-08-23
 */
@Service
public class ShishigaikuangServiceImpl extends ServiceImpl<ShishigaikuangMapper, Shishigaikuang> implements ShishigaikuangService {
    @Autowired
    private YunweiService yunweiService;
    @Autowired
    private JiazhiService jiazhiService;
    @Autowired
    private XiangmuService xiangmuService;
    @Autowired
    private SaidaoService saidaoService;

    @Override
    public Shishigaikuang getData(String state) {
        return this.baseMapper.getData(state);
    }

    @Override
    public void handleImport(MultipartFile file) throws IOException {
        XSSFWorkbook wb = new XSSFWorkbook(file.getInputStream());
        //根据页面index 获取sheet页
        shishigaikuang(wb.getSheet("实施概况"));
        yunweigaikuang(wb.getSheet("运维概况"));
        jiazhi(wb.getSheet("价值反馈"));
        xiangmushishijiankong(wb.getSheet("项目实施监控"),"项目实施监控");
        xiangmushishijiankong(wb.getSheet("项目实施监控"),"场景运维监控");
        xiangmushishijiankong(wb.getSheet("项目实施监控"),"体验日清");
        riqingsaidao(wb.getSheet("日清赛道"));
    }

    private void riqingsaidao(XSSFSheet sheet) {
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i < lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            XSSFCell mingciCell = row.getCell(0);
            String mingci = mingciCell.getStringCellValue() + "";
            XSSFCell xingmingCell = row.getCell(1);
            String xingming = xingmingCell.getStringCellValue() + "";
            XSSFCell dingdanCell = row.getCell(2);
            String dingdan = dingdanCell.getStringCellValue() + "";
            XSSFCell gongdanCell = row.getCell(3);
            String gongdan = gongdanCell.getStringCellValue() + "";
            XSSFCell jiazhifankuiCell = row.getCell(4);
            String jiazhifankui = jiazhifankuiCell.getStringCellValue() + "";
            XSSFCell hejiCell = row.getCell(5);
            String heji = hejiCell.getStringCellValue() + "";
            XSSFCell dabiaolvCell = row.getCell(6);
            String dabiaolv = dabiaolvCell.getStringCellValue() + "";
            Saidao saidao = new Saidao();
            saidao.setMingci(mingci);
            saidao.setXingming(xingming);
            saidao.setDingdan(dingdan);
            saidao.setGongdan(gongdan);
            saidao.setJiazhifankui(jiazhifankui);
            saidao.setHeji(heji);
            saidao.setHeji(dabiaolv);
            saidaoService.save(saidao);
        }
    }

    private void xiangmushishijiankong(XSSFSheet sheet,String type) {
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i < lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            XSSFCell areaNameCell = row.getCell(0);
            String areaName = areaNameCell.getStringCellValue() + "";
            XSSFCell changjingCell = row.getCell(1);
            String changjing = changjingCell.getStringCellValue() + "";
            XSSFCell fenleiCell = row.getCell(2);
            String fenlei = fenleiCell.getStringCellValue() + "";
            XSSFCell shixiaoCell = row.getCell(3);
            String shixiao = shixiaoCell.getStringCellValue() + "";
            XSSFCell renyuanCell = row.getCell(4);
            String renyuan = renyuanCell.getStringCellValue() + "";
            XSSFCell stateCell = row.getCell(5);
            String state = stateCell.getStringCellValue() + "";
            Xiangmu xiangmu = new Xiangmu();
            xiangmu.setType(type);
            xiangmu.setAreaName(areaName);
            xiangmu.setChangjing(changjing);
            xiangmu.setFenlei(fenlei);
            xiangmu.setShixiao(shixiao);
            xiangmu.setRenyuan(renyuan);
            xiangmu.setState(state);
            xiangmuService.save(xiangmu);
        }
    }

    private void jiazhi(XSSFSheet sheet) {
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i < lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            XSSFCell stateCell = row.getCell(0);
            String state = stateCell.getStringCellValue() + "";
            XSSFCell yemiaoCell = row.getCell(1);
            String yimiao = yemiaoCell.getStringCellValue() + "";
            XSSFCell xueyeCell = row.getCell(2);
            String xueye = xueyeCell.getStringCellValue() + "";
            XSSFCell yangbenCell = row.getCell(3);
            String yangben = yangbenCell.getStringCellValue() + "";
            XSSFCell yaopinCell = row.getCell(4);
            String yaopin = yaopinCell.getStringCellValue() + "";
            XSSFCell yunyanCell = row.getCell(5);
            String yunyan = yunyanCell.getStringCellValue() + "";
            XSSFCell hejiCell = row.getCell(6);
            String heji = hejiCell.getStringCellValue() + "";
            Jiazhi jiazhi = new Jiazhi();
            jiazhi.setState(state);
            jiazhi.setYimiao(yimiao);
            jiazhi.setXueye(xueye);
            jiazhi.setYangben(yangben);
            jiazhi.setYaopin(yaopin);
            jiazhi.setYunyan(yunyan);
            jiazhi.setHeji(heji);
            jiazhiService.save(jiazhi);
        }
    }

    private void yunweigaikuang(XSSFSheet sheet) {
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i < lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            XSSFCell stateCell = row.getCell(0);
            String state = stateCell.getStringCellValue() + "";
            XSSFCell yemiaoCell = row.getCell(1);
            String yimiao = yemiaoCell.getStringCellValue() + "";
            XSSFCell xueyeCell = row.getCell(2);
            String xueye = xueyeCell.getStringCellValue() + "";
            XSSFCell yangbenCell = row.getCell(3);
            String yangben = yangbenCell.getStringCellValue() + "";
            XSSFCell yaopinCell = row.getCell(4);
            String yaopin = yaopinCell.getStringCellValue() + "";
            XSSFCell yunyanCell = row.getCell(5);
            String yunyan = yunyanCell.getStringCellValue() + "";
            XSSFCell hejiCell = row.getCell(6);
            String heji = hejiCell.getStringCellValue() + "";
            Yunwei yunwei = new Yunwei();
            yunwei.setState(state);
            yunwei.setYimiao(yimiao);
            yunwei.setXueye(xueye);
            yunwei.setYangben(yangben);
            yunwei.setYaopin(yaopin);
            yunwei.setYunyan(yunyan);
            yunwei.setHeji(heji);
            yunweiService.save(yunwei);
        }
    }

    private void shishigaikuang(XSSFSheet sheet){
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i < lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            XSSFCell stateCell = row.getCell(0);
            String state = stateCell.getStringCellValue() + "";
            XSSFCell yemiaoCell = row.getCell(1);
            String yimiao = yemiaoCell.getStringCellValue() + "";
            XSSFCell xueyeCell = row.getCell(2);
            String xueye = xueyeCell.getStringCellValue() + "";
            XSSFCell yangbenCell = row.getCell(3);
            String yangben = yangbenCell.getStringCellValue() + "";
            XSSFCell yaopinCell = row.getCell(4);
            String yaopin = yaopinCell.getStringCellValue() + "";
            XSSFCell yunyanCell = row.getCell(5);
            String yunyan = yunyanCell.getStringCellValue() + "";
            XSSFCell hejiCell = row.getCell(6);
            String heji = hejiCell.getStringCellValue() + "";
            Shishigaikuang shishigaikuang = new Shishigaikuang();
            shishigaikuang.setState(state);
            shishigaikuang.setYimiao(yimiao);
            shishigaikuang.setXueye(xueye);
            shishigaikuang.setYangben(yangben);
            shishigaikuang.setYaopin(yaopin);
            shishigaikuang.setYunyan(yunyan);
            shishigaikuang.setHeji(heji);
            this.save(shishigaikuang);
        }
    }

}
